package com.ms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.dao.NoticeMapper;
import com.ms.entity.Notice;
import com.ms.service.inter.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 保存通知
     * @param notice 通知对象
     * @return 保存结果
     */
    @Override
    public boolean save(Notice notice) {
        try {
            int result = noticeMapper.insert(notice);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("保存通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据ID查找通知
     * @param id 通知ID
     * @return 通知对象
     */
    @Override
    public Notice findById(Integer id) {
        try {
            return noticeMapper.selectById(id);
        } catch (Exception e) {
            throw new RuntimeException("根据ID查找通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 更新通知
     * @param notice 通知对象
     * @return 更新结果
     */
    @Override
    public boolean update(Notice notice) {
        try {
            int result = noticeMapper.updateById(notice);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("更新通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 删除通知
     * @param id 通知ID
     * @return 删除结果
     */
    @Override
    public boolean delete(Integer id) {
        try {
            int result = noticeMapper.deleteById(id);
            return result > 0;
        } catch (Exception e) {
            throw new RuntimeException("删除通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 获取所有通知
     * @return 通知列表
     */
    @Override
    public List<Notice> findAll() {
        try {
            return noticeMapper.selectList(null);
        } catch (Exception e) {
            throw new RuntimeException("获取所有通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据接收者类型获取通知
     * @param receiverType 接收者类型
     * @return 通知列表
     */
    @Override
    public List<Notice> findByReceiverType(Integer receiverType) {
        try {
            return noticeMapper.selectList(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Notice>()
                            .eq("receiver_type", receiverType)
            );
        } catch (Exception e) {
            throw new RuntimeException("根据接收者类型获取通知失败: " + e.getMessage(), e);
        }
    }

    /**
     * 根据是否审核获取通知
     * @param isUsing 是否审核通过
     * @return 通知列表
     */
    @Override
    public List<Notice> findByIsUsing(Boolean isUsing) {
        try {
            return noticeMapper.selectList(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Notice>()
                            .eq("isUsing", isUsing)
            );
        } catch (Exception e) {
            throw new RuntimeException("根据是否审核获取通知失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 根据发送者类型和发送者ID获取通知
     * @param senderType 发送者类型
     * @param senderId 发送者ID
     * @return 通知列表
     */
    @Override
    public List<Notice> findBySenderTypeAndSenderId(Integer senderType, Integer senderId) {
        try {
            return noticeMapper.selectList(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Notice>()
                            .eq("sender_type", senderType)
                            .eq("sender_id", senderId)
            );
        } catch (Exception e) {
            throw new RuntimeException("根据发送者类型和发送者ID获取通知失败: " + e.getMessage(), e);
        }
    }
}